<?php
class Groups extends CI_Controller{
	
	function Groups()
	{
		parent::__construct();
		
		$this->_container ='layout/container';
		$this->load->model('groupmodel');
		$this->load->model('contact_group_model');
	}
	
	function index()
	{
		redirect('contacts/show');
		$data['heading'] = 'Contacts';
        $data['fal'] = '';
        $data['navigation'] = false;
        $data['header'] = true;
        $data['menu_sms'] = false;
        $data['menu_contact'] = true;
        $data['menu_feedback'] = false;
        $this->load->vars($data);

		$this->load->view($this->_container);
	}
	
	// add and insert the group name to the selected contacts
	function send()
	{
		$group_id = $this->input->post('group-id',true);
		if(!$group_id) // if proup id is not passed then add the group and get new group id 
		{
			$groupname = $this->input->post('group-name',true);
			$insert = array('title'		=> $groupname,
							'user_id'	=> $this->tank_auth->get_user_id());
			$group_id = $this->groupmodel->insert($insert);
		}
		$contactNos = $this->input->post('nos',true);
		if(!empty($contactNos))
		{
			foreach($contactNos as $contactId)
			{
				$insertdata = array('group_id'		=> $group_id,
									'contact_id'	=> $contactId
				);
				$this->contact_group_model->insert($insertdata);
			}
		}
		$json_array = array('success'	=> 'true','login'=>'true');
		echo json_encode($json_array);
	}
	function add()
	{
		$data['heading'] = 'Contacts';
        $data['sms_content'] = $this->contacts_lib->addContact();
        $this->load->view('layout/container', $data);
	}
}